#include<bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int n,ch[1000005][2],f[1000005],ans[1000005],ch2[1000005][2],f2[1000005],dep[1000005];
bool F2=1;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
inline bool ck(int k){
	return (ch[f[k]][1]==k);
}
inline void xuan(int k){
	bool g=ck(k),G=ck(f[k]);
	if(ch[k][g^1]) f[ch[k][g^1]]=f[k];
	ch[f[k]][g]=ch[k][g^1];
	ch[k][g^1]=f[k];
	int d=f[k];
	f[k]=f[d];
	ch[f[d]][G]=k;
	if(d) f[d]=k;
}
inline void splay(int k){
	while(f[k]){
		if(!f[f[k]]) xuan(k);
		else if(ck(k)==ck(f[k])){
			xuan(f[k]);
			xuan(k);
		}
		else xuan(k),xuan(k);
	}
}
inline void dfs(int k,int g){
	ans[k]=(ans[k]+g)%M;dep[k]=g;
	if(ch[k][0]) dfs(ch[k][0],g+1);
	if(ch[k][1]) dfs(ch[k][1],g+1);
} 
inline void solve1(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++) ch[j][0]=ch2[j][0],ch[j][1]=ch2[j][1],f[j]=f2[j];
		splay(i);
		dfs(i,0);
//		for(int j=1;j<=n;j++) cerr<<dep[j]<<" ";
//		cerr<<"\n";
	}
}
inline void solve2(){
	for(int i=1;i<=n;i++){
		ans[i]=(i*(i-(int)1)/(int)2)%M;
		int d=n-i,d1,d2,o=(i-1)/2+1;
		if(d&1) d2=d/2,d1=d-d2;
		else d1=d2=d/2;
		ans[i]=(ans[i]+((d1*o)%M))%M;
		if(i&1) o++;
		else o+=2;
		ans[i]=(ans[i]+((d2*o)%M))%M;
	}
}
signed main(){
	freopen("dream.in","r",stdin);
	freopen("dream.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++){
		ch[i][0]=read(),ch[i][1]=read();
		if(ch[i][0]) f[ch[i][0]]=i;
		if(ch[i][1]) f[ch[i][1]]=i,F2=0;
	}
	for(int i=1;i<=n;i++) ch2[i][0]=ch[i][0],ch2[i][1]=ch[i][1],f2[i]=f[i];
	if(n<=2000) solve1();
	else if(F2) solve2();
	for(int i=1;i<=n;i++) cout<<ans[i]<<"\n";
	return 0;
}
/*
2 
3 3

8 
10 10 
15 17 17 15

16 
26 26 
35 39 39 35 
47 50 53 53 53 53 50 47 

40 
54 54 
79 89 89 79 
103 108 116 116 116 116 108 103 
131 135 138 139 145 147 146 145 145 146 147 145 139 138 135 131 
*/
